<?php
namespace app\common\model;
use think\Db;
class Charts{
    public $year,$month,$day,$start_time,$end_time;

    public function __construct(){        
        $this->year = date("Y",strtotime("-1 day"));
        $this->month = date("m",strtotime("-1 day"));
        $this->day = date("d",strtotime("-1 day"));
        $this->start_time = strtotime($this->year.'-'.$this->month.'-'.$this->day.' '.'00:00:00');
        $this->end_time = strtotime($this->year.'-'.$this->month.'-'.$this->day.' '.'24:59:59');
    }

    public function buquan(){
        $year = date('Y');
        $data = array(
            '01'  => $this->getCurMonthLastDay($year.'-01-01'),
            '02'  => $this->getCurMonthLastDay($year.'-02-01'),
            '03'  => $this->getCurMonthLastDay($year.'-03-01'),
            '04'  => $this->getCurMonthLastDay($year.'-04-01'),
            '05'  => $this->getCurMonthLastDay($year.'-05-01'),
            '06'  => $this->getCurMonthLastDay($year.'-06-01'),
            '07'  => $this->getCurMonthLastDay($year.'-07-01'),
            '08'  => $this->getCurMonthLastDay($year.'-08-01'),
            '09'  => $this->getCurMonthLastDay($year.'-09-01'),
            '10' => $this->getCurMonthLastDay($year.'-10-01'),
            '11' => $this->getCurMonthLastDay($year.'-11-01'),
            '12' => $this->getCurMonthLastDay($year.'-12-01'),
        );
        foreach ($data as $key => $val) {
            $d = $val+1;
            for ($i = 1; $i < $d; $i++) {
                $this->year = $year;
                $this->month = $key;
                $this->day = $i;
                $this->start_time = strtotime($this->year.'-'.$this->month.'-'.$this->day.' '.'00:00:00');
                $this->end_time = strtotime($this->year.'-'.$this->month.'-'.$this->day.' '.'24:59:59');
                $this->init();
            }
        }
    }

    public function getCurMonthLastDay($date) {
        return date('d', strtotime(date('Y-m-01', strtotime($date)) . ' +1 month -1 day'));
    }

    public function init(){
        $rs = Db::name('charts')->where('y',$this->year)->where('m',$this->month)->where('d',$this->day)->select();
        if ( $rs ) return;
        $nows = strtotime(date('Y-m-d'));
        $times = strtotime($this->year.'-'.$this->month.'-'.$this->day);
        if ( $times>=$nows ) return;

        $money = $this->countMoney();
        $gw_money = $this->countGwmoney();
        $pv = $this->countPv();
        $award_lp = $this->countAwardlp();
        $award_tj = $this->countAwardtj();
        $award_gl = $this->countAwardgl();
        $award_bd = $this->countAwardbd();
        $award_fl = $this->countAwardfl();
        $data = [
            'y' => $this->year,
            'm' => $this->month,
            'd' => $this->day,
            'money'    => $money,
            'gw_money' => $gw_money,
            'pv'       => $pv,
            'award_lp' => $award_lp,
            'award_tj' => $award_tj,
            'award_gl' => $award_gl,
            'award_bd' => $award_bd,
            'award_fl' => $award_fl,
            'create_time' => $this->start_time
        ];
        if ( ($money+$gw_money)==0 ) {
            $data['percent'] = 0;
        }else{
            $data['percent'] = bcdiv(($award_lp+$award_gl+$award_bd),($pv),2);
        }
        Db::name('charts')->insert($data);
    }
    #统计奖励积分
    private function countMoney(){
        $rs = Db::query("select sum(money) as total from bill where type in(100,102) and create_time>=? and create_time<=?",[$this->start_time,$this->end_time]);
        return $rs[0]['total']==''?0:$rs[0]['total'];
    }
    #统计购物积分
    private function countGwmoney(){
        $rs = Db::query("select sum(gw_money) as total from bill where type=? and create_time>=? and create_time<=?",[100,$this->start_time,$this->end_time]);
        return $rs[0]['total']==''?0:$rs[0]['total'];
    }
    #统计新增业绩
    private function countPv(){
        $rs = Db::query("select sum(pv) as total from order_user where create_time>=? and create_time<=?",[$this->start_time,$this->end_time]);
        //t(Db::getLastSql());
        return $rs[0]['total']==''?0:$rs[0]['total'];
    }
    #统计推广奖
    private function countAwardlp(){
        $rs = Db::query("select sum(total_money) as total from award_lp where create_time>=? and create_time<=?",[$this->start_time,$this->end_time]);
        return $rs[0]['total']==''?0:$rs[0]['total'];
    }
    #统计推荐奖
    private function countAwardtj(){
        $rs = Db::query("select sum(total_money) as total from award_tj where create_time>=? and create_time<=?",[$this->start_time,$this->end_time]);
        return $rs[0]['total']==''?0:$rs[0]['total'];
    }
    #统计管理奖
    private function countAwardgl(){
        $rs = Db::query("select sum(money) as total from bill where type=? and create_time>=? and create_time<=?",[203,$this->start_time,$this->end_time]);
        return $rs[0]['total']==''?0:$rs[0]['total'];
    }
    #统计报单奖
    private function countAwardbd(){
        $rs = Db::query("select sum(money) as total from bill where type=? and create_time>=? and create_time<=?",[204,$this->start_time,$this->end_time]);
        return $rs[0]['total']==''?0:$rs[0]['total'];
    }
    #统计返利奖
    private function countAwardfl(){
        $rs = Db::query("select sum(money) as total from bill where type=? and create_time>=? and create_time<=?",[205,$this->start_time,$this->end_time]);
        return $rs[0]['total']==''?0:$rs[0]['total'];
    }
    
}